﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AsyncTcp.Server;
using FighttheLandOwner.GameCore;

namespace Server
{
    class Program
    {
        static int whoplay = 0;

        static int readyplayer = 0;

        /// <summary>
        /// 玩家列表
        /// </summary>
        static List<string> Player = new List<string>();
        /// <summary>
        /// 上家出的牌
        /// </summary>
        static List<Card> DeskCards = new List<Card>();

        static AsyncTcpServer server;
        static void Main(string[] args)
        {
            Console.WriteLine("server is starting on port 2552...");
            server = new AsyncTcpServer(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2552));
            server.ClientConnected += Server_ClientConnected;
            server.PlaintextReceived += Server_PlaintextReceived;
            server.Start();
            Console.WriteLine("server started.");
            while (true) ;
        }

        private static void Server_PlaintextReceived(object sender, TcpDatagramReceivedEventArgs<string> e)
        {
            var msg = e.Datagram.Split('|');
            switch (msg[0])
            {
                case "Log":
                    if (!Player.Contains(msg[1]))
                    {
                        Log("player " + msg[1] + " logged in.");
                        server.SendAll("ok");
                        Player.Add(msg[1]);
                    }
                    else
                    {
                        server.SendAll("userexist|" /*+ msg[1]*/);
                    }
                    break;
                case "Start":
                    Log("game started.");
                    StartGame();
                    break;
                case "putcard":
                    //出牌
                    if(msg[1]!="null")
                    {
                        DeskCards = CardConvert.ToCards(msg[1]);
                        string cnt = msg[2];
                        server.SendAll("deskcard|" + CardConvert.ToString(DeskCards)+"|"+Player[whoplay]+"|"+cnt);//将上家打的牌上到桌子上
                    }
                    if (whoplay < 2) whoplay++;
                    else whoplay = 0;
                    server.SendAll("youplay|" + Player[whoplay]);
                    break;
                case "win":
                    server.SendAll("win|" + msg[1]);
                    break;
                case "ready":
                    readyplayer++;
                    if(readyplayer==3)
                    {
                        Log("Game Ready");
                        server.SendAll("canstart|" + msg[1]);
                    }
                    break;
                case "unready":
                    readyplayer--;
                    break;
                case "exit":
                    server.SendAll("gameabort|");
                    break;
            }

        }

        private static void Server_ClientConnected(object sender, TcpClientConnectedEventArgs e)
        {

        }

        static void Log(string msg)
        {
            DateTime time = DateTime.Now;
            Console.WriteLine(time.ToString("[HH:mm:ss]") + msg);
        }

        /// <summary>
        /// 开始游戏
        /// </summary>
        static void StartGame()
        {
            CardManager cardManager = new CardManager();
            var cards = cardManager.HandOutCards();
            Random r = new Random();
            int indexofLord = r.Next(0, 3);//选地主
            if (indexofLord == 0) cards.Player1.AddRange(cards.LeftCards);
            if (indexofLord == 1) cards.Player2.AddRange(cards.LeftCards);
            if (indexofLord == 2) cards.Player3.AddRange(cards.LeftCards);

            //发底牌
            server.SendAll("leftcards|" + CardConvert.ToString(cards.LeftCards));
            Thread.Sleep(100);
            //发送玩家列表
            server.SendAll("players|" + Player[0] + "|" + Player[1] + "|" + Player[2]);
            Thread.Sleep(100);
            //发送地主名字
            server.SendAll("lord|" + Player[indexofLord]);
            Thread.Sleep(100);
            //发送玩家的牌
            server.SendAll(Player[0] + "|" + CardConvert.ToString(cards.Player1));
            Thread.Sleep(100);
            server.SendAll(Player[1] + "|" + CardConvert.ToString(cards.Player2));
            Thread.Sleep(100);
            server.SendAll(Player[2] + "|" + CardConvert.ToString(cards.Player3));

            whoplay = indexofLord;
        }
    }
}
